<?php
/**
 * Created by PhpStorm.
 * User: sx-2
 * Date: 2016/9/8
 * Time: 10:17
 */
class items_mdl extends Model{

    public static $item_table = 'mac_tm_items';
    public static $rates_table = 'mac_rates';

    //添加和更新json 
    public function add_json($num_iid,
                                $seller_cids,
                                $title,
                                $price,
                                $ump_price,
                                $period_sold_quantity,
                                $item_imgs,
                                $skus){

        $item = $this->get_item_numiid($num_iid);
        if(empty($item['num_iid'])){
            $this->dbh->insert(self::$item_table,
                array(
                    'num_iid' => $num_iid,
                    'seller_cids' => $seller_cids,
                    'title' => $title,
                    'price' => $price,
                    'ump_price' => $ump_price,
                    'period_sold_quantity' => $period_sold_quantity,
                    'item_imgs' => $item_imgs,
                    'skus' => $skus,
                    'last_time' => date('Y-m-d H:i:s'),
                    'create_time' => date('Y-m-d H:i:s')
                )
            );
            $rs_id = $this->dbh->insert_id();
        }else {
            $this->dbh->update(self::$item_table,
                array(
                    'seller_cids' => $seller_cids,
                    'title' => $title,
                    'price' => $price,
                    'period_sold_quantity' => $period_sold_quantity,
                    //'item_imgs' => $item_imgs,
                    'skus' => $skus,
                    'ump_price' => $ump_price,
                    'last_time' => date('Y-m-d H:i:s')
                ),
                'num_iid=' . $num_iid
            );
            $item = $this->get_item_numiid($num_iid);
        }
        return $item;
    }

    
    public function get_item_skuid($sku_id){
        $r = '';

        if(!empty($sku_id)){
            $sql = 'SELECT *
					FROM '.self::$item_table.'
					WHERE sku_id = '.$sku_id;
            $r = $this->dbh->get_one($sql);
        }

        return $r;
    }
    
    public function get_item_numiid($num_iid,$feild="*"){
        $r='';

        if(!empty($num_iid)){
            $sql = "SELECT DISTINCT $feild FROM ".self::$item_table.' WHERE num_iid = '.$num_iid;
            $r = $this->dbh->get_one($sql);
        }

        return $r;
    }

    //分页查询所有商品
     public function get_items($pageno = 1,$pagesize = 10, $catsId, $keyword, $fields='*')
     {
         $sql_where = '';
         if(gettype($catsId) == 'array'){
             $sql_where .= " AND (";
             for($i=0;$i<count($catsId);$i++){
                 if($i < count($catsId) - 1){
                     $sql_where .= " A.seller_cids = ".$catsId[$i]." OR " ;
                 }else{
                     $sql_where .= " A.seller_cids = ".$catsId[$i]." " ;
                 }
             }
             $sql_where .= " ) ";
         }else{
             $sql_where .= (!empty($catsId))? " AND A.num_iid = ".$catsId : "";
         }

         $sql_where .= (!empty($keyword))? " AND A.title LIKE '%".$keyword ."%'": "";
         $sql = 'SELECT A.num_iid
				 FROM '.self::$item_table.' A
				 WHERE 1 = 1
				 '.$sql_where;

         $this->dbh->get_all($sql);
         $count = $this->dbh->rows_count;

         /*$sql = 'SELECT '.$fields.'
				 FROM '.self::$item_table.'
				 WHERE 1 = 1
				 '.$sql_where.'
				  ORDER BY create_time DESC
				 LIMIT '.(($pageno-1)*$pagesize).' , '.$pagesize;*/


         $sql = 'SELECT '.$fields.' 
                  FROM '.self::$item_table.' A 
                  LEFT JOIN 
                  '.self::$rates_table.' B 
                  ON A.num_iid=B.item_id 
                  WHERE 1 = 1
				 '.$sql_where.'
				  ORDER BY last_time DESC
				 LIMIT '.(($pageno-1)*$pagesize).' , '.$pagesize;

         $rs = $this->dbh->get_all($sql);

         return array(
             "total"=>$count,
             "list"=>$rs
         );
     }

    //查所有商品ID
    public function get_items_id()
    {
        $sql = 'SELECT num_iid
				 FROM '.self::$item_table;

        $this->dbh->get_all($sql);
        $count = $this->dbh->rows_count;

        //$pageno = 1;
        $pagesize = 200;

        $ids = array();

        $pages = ceil($count / $pagesize);

        for($pageno = 1;$pageno <= $pages; $pageno++){
            $sql = 'SELECT num_iid
                FROM '.self::$item_table.'
                LIMIT '.(($pageno-1)*$pagesize).' , '.$pagesize;

            $rs = $this->dbh->get_all($sql);
            array_push($ids, array_column($rs, 'num_iid'));
        }

        return array(
            "total"=>$count,
            "list"=>$ids[0]
        );
    }

    //修改商品
    public function set_item($num_iid,$item_imgs){

            $this->dbh->update(self::$item_table,
                array(
                    'item_imgs' => $item_imgs,
                    'last_time' => date('Y-m-d H:i:s')
                ),
                'num_iid=' . $num_iid
            );
            $item = $this->get_item_numiid($num_iid);

            return $item;
    }

    //删除不存在的商品
    public function del_items_ids($del_ids)
    {
        $result = '';
        foreach ($del_ids as $id){
            $result = $this->dbh->delete(self::$item_table," num_iid = ".$id);
        }
        return $result;
    }


}